﻿/*
 * Crée par SharpDevelop.
 * Utilisateur: ngr
 * Date: 11/02/2015
 * Heure: 16:32
 * 
 * Pour changer ce modèle utiliser Outils | Options | Codage | Editer les en-têtes standards.
 */
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;


using Compta.Core.Classes;
using Compta.Core.Structures;
using Compta.Core;

namespace ComptaPerso.Controls
{
	/// <summary>
	/// Description of UC_Account.
	/// </summary>
	public partial class UC_Account : UserControl
	{
		Account MyAccount; 
		ComptaCore core;
		
		MainForm m;
		UC_AccountConf AConf;
		
		//Devise devise;
		
		public UC_Account(MainForm m, int AccountId)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			//this.splitContainer1.SplitterDistance = 153;
			
			//init
			this.m = m;
			this.core = m.core;
			MyAccount = new Account();
			MyAccount.Id = AccountId;
			this.AConf = new UC_AccountConf();
			
			Commun();
		}
		
		public UC_Account(MainForm m, Compta.Core.Classes.Account acc)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			//this.splitContainer1.SplitterDistance = 153;
			
			//init
			this.m = m;
			this.core = m.core;
			MyAccount = acc;
			this.AConf = new UC_AccountConf();
			
			Commun();
			
			
		}
		
		
		void Commun()
		{
			LoadAccount();
			ShowAccountInfo();
			LoadOperations();
		}
		
		/// <summary>
		/// Chargement du compte
		/// </summary>
		void LoadAccount()
		{
			foreach (Account a in core.ListAccounts)
			{
				if (a.Id.Equals(MyAccount.Id))
				{
					MyAccount = a;
					break;
				}
			}
		}
			
		
		void ShowAccountInfo()
		{
			lblBanque.Text = MyAccount.Bank;
			lblNomCompte.Text = MyAccount.Name;
			lblNomTitulaire.Text = MyAccount.Owner;
			lblSoldeP.Text = MyAccount.BalanceProjected.ToString();
			lblSoldeR.Text = MyAccount.BalanceReal.ToString();

		}
		
		
		void LoadAccountParametres()
		{
			
			
		}
		

		/// <summary>
		/// Ajout d'une opération
		/// </summary>
		void AddOperation()
		{
			// affichage ihm de saisie
			PopUp pop = new PopUp();
			UC_Operation myop = new UC_Operation(core, MyAccount,pop);
			myop.Title.Text = "Nouvelle opération pour le compte \"" + MyAccount.Name + "\"";
			//pop.AddControl(myop);
			// l'ajout du control se fait dans le controle
			pop.ShowDialog();	
			
			// on ajoute l'opération à la liste
			if (!myop.Cancel){
				AddOpLine(myop.Op);
					
				LoadAccount();
				ShowAccountInfo();
				
			}
			
				
			pop.Dispose();
			
		}
		
		
		/// <summary>
		/// chargement des opérations du comptes
		/// triées de la plus récente à la plus ancienne
		/// </summary>
		void LoadOperations()
		{
			MyAccount.GetOperations();
			
			foreach (Operation op in MyAccount.Operations)
			{
				AddOpLine(op);
				
			}
		}
		
		void AddOpLine(Operation op)
		{
			// date
			ListViewItem lvi = new ListViewItem(op.OpDate.ToString("yyyy-MM-dd"));
			
			// dénomination
			lvi.SubItems.Add(op.Denomination_Label);
			
			// categorie
			Denomination dnm = core.GetDenominationById(op.Denomination_Id);
			Categorie ctg = core.GetCategorieById(dnm.Categorie_Id);
			lvi.SubItems.Add(ctg.Label);
			
			// mode paiement
			lvi.SubItems.Add(op.ModePai);			

			// référence
			lvi.SubItems.Add(op.Reference);
			
			// debit
			lvi.SubItems.Add(op.debit.ToString());
			
			// credit
			lvi.SubItems.Add(op.credit.ToString());
			
			// pointé
			lvi.SubItems.Add(op.isChecked.ToString());
			
			// id de l'op
			lvi.SubItems.Add(op.Id.ToString());
				
			
			// ajout de la ligne
			lstOps.Items.Add(lvi);
			
			
		}
				
		/// <summary>
		/// Suppression d'une opération
		/// </summary>
		void DelOperation(int opId)
		{
			
		}
		
		/// <summary>
		/// Modification d'une opération
		/// </summary>
		void ModOperation(int opId)
		{
			
		}
		
		
	
		
		
		void SplitContainer1SizeChanged(object sender, EventArgs e)
		{
		//	splitContainer1.SplitterDistance = 153;
		}
		
		void BtnCloseAccountClick(object sender, EventArgs e)
		{
			this.Dispose();
			
		}
		

		
		void LblSoldePTextChanged(object sender, EventArgs e)
		{
			if (!lblSoldeP.Text.Equals(lblSoldeR.Text))
			{
				// on met en gras
				lblSoldeP.Font = new Font(lblSoldeP.Font, FontStyle.Bold);
				lblSoldeR.Font = new Font(lblSoldeR.Font, FontStyle.Bold);
			}
			
			if (lblSoldeP.Text[0].Equals('-'))
			{
				// on met en rouge
				lblSoldeP.ForeColor = Color.Red;
				
			}
			else{
				lblSoldeP.ForeColor = Color.Green;
			}
		}
		
		void LblSoldeRTextChanged(object sender, EventArgs e)
		{
			if (!lblSoldeP.Text.Equals(lblSoldeR.Text))
			{
				// on met en gras
				lblSoldeP.Font = new Font(lblSoldeP.Font, FontStyle.Bold);
				lblSoldeR.Font = new Font(lblSoldeR.Font, FontStyle.Bold);
			}
			
			if (lblSoldeR.Text[0].Equals('-'))
			{
				// on met en rouge
				lblSoldeR.ForeColor = Color.Red;
				
			}
			else{
				lblSoldeR.ForeColor = Color.Green;
			}
		}
	}
}
